  
import plugin from '../../lib/plugins/plugin.js';

export class RandomSuperpower extends plugin {
  constructor() {
    super({
      name: '随机超能力',
      dsc: '随机获取并发送超能力信息',
      event: 'message',
      priority: 6,
      rule: [
        {
          reg: '^(#|/)?(超能力|随机超能力)$',
          fnc: 'generateSuperpower'
        },
      ]
    });
  }
  async generateSuperpower(e) {
    const url = 'http://baikeji.xyz/super/index.php';
    try {
      const response = await fetch(url);
      if (!response.ok) {
        throw new Error(`Failed to fetch data from ${url}. Status code: ${response.status}`);
      }

      const textResult = await response.text();
      const [superpower, ...rest] = textResult.split('。').map((item) => item.trim());
      let sideEffect, hiddenItem;

      for (const item of rest) {
        if (item.startsWith('副作用：')) {
          sideEffect = item.replace('副作用：', '');
        } else if (item.startsWith('隐藏款：')) {
          hiddenItem = item.replace('隐藏款：', '');
        }
      }

      const formattedText = `
**${superpower}**
${sideEffect ? `#### 副作用${sideEffect}\n` : ''}
${hiddenItem ? `#### 隐藏款${hiddenItem}\n` : ''}
`.trim();
      const button = segment.button([
        { text: '我也要玩', input: `#随机超能力`, send: true },
      ]);

      const replyContent = [
        formattedText,
        button,
      ];
      await this.reply(replyContent);
      return true; // 返回 true 阻挡消息不再往下
    } catch (error) {
      console.error('Error fetching and generating random superpower:', error);
      await this.reply('抱歉，获取超能力信息时发生错误。请稍后再试。');
      return true; // 返回 true 阻挡消息不再往下
    }
  }
  async reply(text) {
    return await this.e.reply(text, false);
  }
}
 